<!DOCTYPE html>
<html>
<head>
    <title>user form</title>
    <#include "/header.html">
</head>
<body class="bg-white">

<div id="appMain">
</div>

<script id="formTpl" type="text/html">
    <form class="layui-form layuimini-form" action="" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input" autocomplete="off">
            </div>
        </div>
        {{#  if(d.formDataId > 0){ }}
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" id="password" name="password" lay-verify="password" placeholder="留空则不改" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" name="password_confirm" lay-verify="password_confirm" placeholder="留空则不改" value="" class="layui-input">
            </div>
        </div>
        {{#  }else{ }}
        <div class="layui-form-item">
            <label class="layui-form-label required">密码</label>
            <div class="layui-input-block">
                <input type="password" id="password" name="password" lay-verify="required|password" lay-reqtext="密码不能为空" placeholder="请输入密码" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">确认密码</label>
            <div class="layui-input-block">
                <input type="password" name="password_confirm" lay-verify="required|password_confirm" lay-reqtext="密码不能为空" placeholder="请再次输入密码" value="" class="layui-input">
            </div>
        </div>
        {{#  } }}
        <div class="layui-form-item">
            <label class="layui-form-label required">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="mobile" lay-verify="required|phone" lay-reqtext="手机号不能为空" placeholder="请输入手机号" value="" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" placeholder="请输入邮箱" value="" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="0" title="禁用">
                <input type="radio" name="status" value="1" title="启用" checked="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block" id="roleListBox">
                {{#  layui.each(d.list, function(index, item){ }}
                <input type="checkbox" name="roleIdList[{{item.roleId}}]" value="{{item.roleId}}" title="{{item.roleName}}" lay-filter="roleIdCheck">
                {{#  }); }}
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>

        <div class="layui-form-item layui-hide">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" id="commitBtn" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </form>
</script>

<script type="text/javascript">
    layui.use(['form', 'laytpl'], function () {
        var form = layui.form,
            laytpl = layui.laytpl,
            $ = layui.$;

        var d = {formDataId: parent.formDataId, list: parent.roleListData};
        var _parent = parent;
        laytpl($("#formTpl").html()).render(d, function(html){
            $("#appMain").html(html);
            form.render();
            //初始赋值
            if(_parent.formDataId > 0) {
                $.ajax({
                    type: "POST",
                    url: serverUrl('sys/user/info/' + _parent.formDataId),
                    success: function (result) {
                        console.log(result)
                        if (result.code === 0) {
                            var data = result.user;
                            var values = {
                                "username": data.username,
                                "mobile": data.mobile,
                                "email": data.email,
                                "status": data.status === 1,
                                "remark": data.remark
                            };
                            $(data.roleIdList).each(function (i, val) {
                                values['roleIdList['+val+']'] = true;
                            });
                            form.val('dataForm', values);
                        }
                    }
                });
            }
        });

        form.verify({
            password: function(value, item) {
                var p = /^[\S]{6,20}$/;
                if (value.length > 0 && !p.test(value)) {
                    return '密码必须6到20位，且不能出现空格';
                }
            }
            ,password_confirm: function(value, item){
                var data = form.val('dataForm');
                if (value.length > 0 && value != data.password) {
                    return '两次输入的密码不一致';
                }
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            /*alert(JSON.stringify(data));
            return false;*/
            var params = data.field;
            params['userId'] = parent.formDataId || null;
            var roleIdList = [];
            for (var key in params) {
                if (key.indexOf('roleIdList') === 0) {
                    roleIdList.push(params[key]);
                }
            }
            params['roleIdList'] = roleIdList;

            var method = parent.formDataId > 0 ? 'update' : 'save';
            submitFormData('sys/user/'+method, params);

            return false;
        });

    });
</script>
</body>
</html>